package com.bumpslide.ui.skin 
{
	import com.bumpslide.ui.Component;
	import com.bumpslide.ui.IResizable;
	import com.bumpslide.ui.Slider;

	import flash.display.MovieClip;

	/**
	 * Movie Clip SliderSkin
	 *
	 * @author David Knape
	 * @version SVN: $Id: $
	 */
	public class BasicSliderSkin extends BasicSkin implements ISkin 
	{	
		public var handle:MovieClip;
		public var background:MovieClip;
		
		/**
		 * Best Practice: create a typed hostComponent getter for type safety
		 */
		public function get hostComponent() : Slider {
			return _hostComponent as Slider;		
		}
		
		override public function init(host_component:ISkinnable):void
		{	
			super.init(host_component);
			
			if(hostComponent==null) {
				throw new Error( 'SliderSkin is meant to be used with a Slider component.');
			}
			 
			background.buttonMode = true;
			handle.buttonMode = true;
			handle.mouseChildren = false;
						
			// For movie clip skins, we may want to do something like we did
			// in the original slider component...
			
//			// init padding based on position of handle on the stage
//			_pad = new Padding(handle.y, handle.x, handle.y, handle.x);
//			if(isVertical) {
//				_pad.right = _width - handle.getBounds(this).right;
//			} else {
//				_pad.bottom = _height - handle.getBounds(this).bottom;
//			}
//						
//			// for sliders (not scrollbars), use current handle size as the fixed handle size
//			fixedHandleSize = ( _orientation == Direction.HORIZONTAL ) ? handle.width : handle.height;
		
		}
		
		
		override public function destroy():void
		{
			
		}
		
		
		
		
		override public function draw( skinState:String ):void
		{
			if(hostComponent==null) return;
						
			drawBack();
			drawHandle();
		}
		
		/**
		 * Draws the back of the slider.
		 */
		protected function drawBack():void 
		{
			if(background == null) return;
			
			trace('size the background');
			if(background is IResizable) { 
				(background as IResizable).setSize( hostComponent.width, hostComponent.height );
			} else {
				// Assume the background is a movieclip
				background.height = hostComponent.height;
				background.width = hostComponent.width;
			}
		}
		
		/**
		 * Draws the handle of the slider.
		 */
		protected function drawHandle():void 
		{				
			trace('draw handle');
			
			if(handle == null) return;	
			
			var hs:Number = hostComponent.getHandleSize();
			if(hostComponent.isVertical) {
				handle.height = hs;
				handle.width = hostComponent.width-hostComponent.padding.width;
				handle.x = hostComponent.padding.left;
				handle.y = hostComponent.getCalculatedHandlePosition();
			} else {					
				handle.width = hs;
				handle.height = hostComponent.height - hostComponent.padding.height;
				handle.x = hostComponent.getCalculatedHandlePosition();
				handle.y = hostComponent.padding.top;
			}
			if(handle is Component) {
				Component(handle).updateNow();
			}
			
		}
		
	}
}
